package com.darthsith.runtap.activity.fragment;

import java.nio.ByteBuffer;

import android.app.Fragment;
import android.app.Service;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.darthsith.runtap.Constants;
import com.darthsith.runtap.R;
import com.darthsith.runtap.activity.MainActivity;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class GameFragment extends Fragment implements OnClickListener {
	
	private ImageView leftFoot;
	private ImageView rightFoot;
	private TextView counter;
	private TextView countdown;
	private Integer stepNumber;
	private CountDownTimer timer;
	private Integer gameDuration;
	private Vibrator vibrator;
//	private MediaPlayer mediaPlayer;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view= inflater.inflate(R.layout.game_fragment, container, false);
		gameDuration=PreferenceManager.getDefaultSharedPreferences(getActivity()).getInt(Constants.PREF_GAMEDURATION, Constants.DEFAULT_GAMEDURATION);
		leftFoot=(ImageView) view.findViewById(R.id.left_foot);
		rightFoot=(ImageView) view.findViewById(R.id.right_foot);
		counter=(TextView) view.findViewById(R.id.counter);
		countdown=(TextView) view.findViewById(R.id.timer);
		AdView adView = (AdView)view.findViewById(R.id.adView);
	    AdRequest adRequest = new AdRequest.Builder().build();
	    adView.loadAd(adRequest);
		leftFoot.setOnClickListener(this);
		rightFoot.setOnClickListener(this);
		leftFoot.setEnabled(false);
		rightFoot.setEnabled(false);
		stepNumber=Integer.valueOf(0);
		vibrator=(Vibrator) getActivity().getSystemService(Service.VIBRATOR_SERVICE);
//		mediaPlayer=MediaPlayer.create(getActivity(), R.raw.step);
		Log.i(getTag(), "Load GameFragment");
		return view;
	}
	
	@Override
	public void onStart() {
		super.onStart();
		boolean multiplayer=getArguments().getBoolean("m");
		startGame(multiplayer);
	}

	private void startGame(final boolean multiplayer) {
		if (multiplayer) {
			gameDuration=20;
		}
		Log.i(getTag(), "Start Game of "+gameDuration+" seconds");
		timer=new CountDownTimer(gameDuration*1000, 1000) {
			
			@Override
			public void onTick(long millisUntilFinished) {
				gameDuration--;
				updateUI();
			}
			
			@Override
			public void onFinish() {
				gameDuration=0;
				leftFoot.setEnabled(false);
				rightFoot.setEnabled(false);
				vibrator.vibrate(200);
				updateUI();
				if (getActivity()!=null) {
					((MainActivity)getActivity()).uploadScore(stepNumber);
					((MainActivity)getActivity()).incrementAchieve(getString(R.string.achievement_bronze));
					((MainActivity)getActivity()).incrementAchieve(getString(R.string.achievement_silver));
					((MainActivity)getActivity()).incrementAchieve(getString(R.string.achievement_gold));
					((MainActivity)getActivity()).incrementAchieve(getString(R.string.achievement_platinum));
				}
				if (multiplayer) {
					//incapsulo il numero passi in un array di byte
					ByteBuffer byteBuffer=ByteBuffer.allocate(4);
					byte[] buf=byteBuffer.putInt(stepNumber).array();
					((MainActivity)getActivity()).sendMsg(buf);
				}
				else {
					if (getActivity()!=null)
						((MainActivity)getActivity()).buildAlert("Partita Finita!\nHai fatto "+stepNumber+" passi");
				}
			}
		};
		leftFoot.setEnabled(true);
		rightFoot.setEnabled(true);
		timer.start();
	}
	
	@Override
	public void onClick(View v) {
		if (v.getId()==leftFoot.getId()) {
			leftFoot.setEnabled(false);
			rightFoot.setEnabled(true);
			stepNumber++;
//			mediaPlayer.start();
			updateUI();
		}
		if (v.getId()==rightFoot.getId()) {
			leftFoot.setEnabled(true);
			rightFoot.setEnabled(false);
			stepNumber++;
//			mediaPlayer.start();
			updateUI();
		}
		if (stepNumber==100) {
			((MainActivity)getActivity()).unlockAchieve(getString(R.string.achievement_100_steps));
		}
	}
	
	private void updateUI() {
		String val=stepNumber.toString();
		counter.setText(val);
		String val2=gameDuration.toString();
		countdown.setText(val2);
	}
	
	public Integer getStepNumber() {
		return stepNumber;
	}

}
